<template>
  <div>
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="任务单号" prop="code">
          <el-input v-model="formData.code" placeholder="请输入任务单号"/>
        </el-form-item>
        <el-form-item label="出库单ID" prop="shipOrderId">
          <el-input v-model="formData.shipOrderId" placeholder="请输入出库单ID"/>
        </el-form-item>
        <el-form-item label="发货单号" prop="shipOrderCode">
          <el-input v-model="formData.shipOrderCode" placeholder="请输入发货单号"/>
        </el-form-item>
        <el-form-item label="状态：TO_BE_PICKED-待拣货；PICKING-拣货中；PICK_OVER-拣货完成" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="任务类型：PICK-拣货；LOW_BATTERY-低电量更换标签" prop="taskType">
          <el-select v-model="formData.taskType" placeholder="请选择任务类型：PICK-拣货；LOW_BATTERY-低电量更换标签">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="待更换的低电量RFID编码" prop="rfidCode">
          <el-input v-model="formData.rfidCode" placeholder="请输入待更换的低电量RFID编码"/>
        </el-form-item>
        <el-form-item label="仓库分类：生产仓库-PRODUCTION；三方仓库-THIRD" prop="warehouseType">
          <el-select v-model="formData.warehouseType" placeholder="请选择仓库分类：生产仓库-PRODUCTION；三方仓库-THIRD">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="分派人ID" prop="assignUserId">
          <el-input v-model="formData.assignUserId" placeholder="请输入分派人ID"/>
        </el-form-item>
        <el-form-item label="分派人名" prop="assignUserName">
          <el-input v-model="formData.assignUserName" placeholder="请输入分派人名"/>
        </el-form-item>
        <el-form-item label="拣货开始时间" prop="pickStartTime">
          <el-date-picker clearable v-model="formData.pickStartTime" type="date" value-format="timestamp"
                          placeholder="选择拣货开始时间"/>
        </el-form-item>
        <el-form-item label="拣货完成时间" prop="pickCompleteTime">
          <el-date-picker clearable v-model="formData.pickCompleteTime" type="date" value-format="timestamp"
                          placeholder="选择拣货完成时间"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as PickTaskApi from '@/api/hs/picktask';

export default {
  name: "PickTaskForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        code: undefined,
        shipOrderId: undefined,
        shipOrderCode: undefined,
        status: undefined,
        taskType: undefined,
        rfidCode: undefined,
        warehouseType: undefined,
        assignUserId: undefined,
        assignUserName: undefined,
        pickStartTime: undefined,
        pickCompleteTime: undefined,
      },
      // 表单校验
      formRules: {
        code: [{required: true, message: '任务单号不能为空', trigger: 'blur'}],
        status: [{
          required: true,
          message: '状态：TO_BE_PICKED-待拣货；PICKING-拣货中；PICK_OVER-拣货完成不能为空',
          trigger: 'blur'
        }],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await PickTaskApi.getPickTask(id);
          this.formData = res.data;
          this.title = "修改拣货任务";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增拣货任务";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await PickTaskApi.updatePickTask(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await PickTaskApi.createPickTask(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        code: undefined,
        shipOrderId: undefined,
        shipOrderCode: undefined,
        status: undefined,
        taskType: undefined,
        rfidCode: undefined,
        warehouseType: undefined,
        assignUserId: undefined,
        assignUserName: undefined,
        pickStartTime: undefined,
        pickCompleteTime: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
